package com.yjhb.android.sbz.tool;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.yjhb.android.sbz.App;
import com.yjhb.android.sbz.BuildConfig;
import com.yjhb.android.sbz.Conf;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.mvp.behavior.BehaviorPresenter;
import com.yjhb.android.sbz.mvp.behavior.BehaviorView;
import com.jojo.android.zxlib.interfaces.Callback;
import com.jojo.android.zxlib.tool.ZxClipboardTool;
import com.jojo.android.zxlib.tool.ZxImageTool;
import com.jojo.android.zxlib.tool.ZxToast;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.UiError;

import java.io.File;
import java.io.IOException;

/**
 * 创建者 zx
 * 创建时间 2020/12/30 15:43
 */
public class ShareTool implements BehaviorView {
    private final String TAG = "ShareTool";

    private static ShareTool instance;

    private static final String TITLE = "帮人忙，赚赏金！就来赏帮";
    private static final String CONTENT = "【赏帮】让闲暇时光，创造价值！";
    private static final String URL = (URLConfig.APPDownloadURL == null ? BuildConfig.APPDownloadURL : URLConfig.APPDownloadURL) + "/"+InvitationCodeTool.generateCode();

    public static ShareTool getInstance() {
        if (instance == null) {
            instance = new ShareTool();
        }
        return instance;
    }

    private BehaviorPresenter presenter;

    private ShareTool() {
        presenter = new BehaviorPresenter(this);
    }

    /**
     * 链接分析
     */
    public void shareLink() {
        ZxClipboardTool.copyText(App.getInstance(), "【赏帮】帮人忙，赚赏金！就来赏帮"
                + " "+URL+" "
                +"复制链接，去浏览器中打开");
        presenter.addUserBehaviorCount(4);
    }


    public void shareImgWx(Activity context,int themeIndex) {
        PosterFactory.AsyncPoster(context, URL,
                themeIndex, new Callback<Bitmap>() {
                    @Override
                    public void callback(Bitmap bitmap) {
                        ShareUtils.WxShareImage(context, Conf.WX_APP_ID, 0, bitmap);
                        presenter.addUserBehaviorCount(2);
                    }
                });
    }

    public void shareImgQQ(Activity context,int themeIndex) {
        PosterFactory.AsyncPoster(context, URL,
                themeIndex, new Callback<Bitmap>() {
                    @Override
                    public void callback(Bitmap bitmap) {
                        String path = Tool.saveShareBitmap(bitmap);
                        ShareUtils.QQShareImage(context, Conf.QQ_APP_ID, path, new IUiListener() {
                            @Override
                            public void onComplete(Object o) {
                                presenter.addUserBehaviorCount(3);
                            }

                            @Override
                            public void onError(UiError uiError) {

                            }

                            @Override
                            public void onCancel() {

                            }

                            @Override
                            public void onWarning(int i) {

                            }
                        });

                    }
                });
    }

    public void shareImgPyq(Activity context,int themeIndex) {
        PosterFactory.AsyncPoster(context, URL,
                themeIndex, new Callback<Bitmap>() {
                    @Override
                    public void callback(Bitmap bitmap) {
                        ShareUtils.WxShareImage(context, Conf.WX_APP_ID, 1, bitmap);
                        presenter.addUserBehaviorCount(1);
                    }
                });
    }

    /**
     * 图文分享给微信好友
     *
     * @param activity
     */
    public void shareImgTextWx(Activity activity) {
        shareImgTextWx(activity, 0);
    }

    /**
     * 图文分享到朋友圈
     *
     * @param activity
     */
    public void shareImgTextPyq(Activity activity) {
        shareImgTextWx(activity, 1);
    }

    /**
     * 微信图文分享
     *
     * @param activity
     * @param target   分享目标 0 分享到会话，1 分享到朋友圈
     */
    private void shareImgTextWx(Activity activity, int target) {
        Glide.with(activity)
                .asBitmap()
                .load(R.drawable.wxlogo)
                .into(new CustomTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        ShareUtils.WxShareWeb(activity, Conf.WX_APP_ID, target, URL, TITLE, CONTENT, resource);
                        if (target == 1) {
                            presenter.addUserBehaviorCount(1);
                        }else {
                            presenter.addUserBehaviorCount(2);
                        }
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {

                    }
                });
    }

    /**
     * 分享图文到QQ
     *
     * @param activity
     */
    public void shareImgTextQQ(Activity activity) {
        Glide.with(activity)
                .asBitmap()
                .load(R.mipmap.logo)
                .into(new CustomTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        try {
                            File img = File.createTempFile("" + System.currentTimeMillis(), ".png");
                            ZxImageTool.save(resource, img, Bitmap.CompressFormat.PNG, false);
                            ShareUtils.QQShareImageText(activity, Conf.QQ_APP_ID, TITLE, URL, CONTENT,
                                    img.getAbsolutePath(), new IUiListener() {
                                        @Override
                                        public void onComplete(Object o) {

                                        }

                                        @Override
                                        public void onError(UiError uiError) {
                                            ZxToast.showINFOBottomToast(uiError.errorMessage);
                                        }

                                        @Override
                                        public void onCancel() {

                                        }

                                        @Override
                                        public void onWarning(int i) {

                                        }
                                    });
                            presenter.addUserBehaviorCount(3);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {

                    }
                });


    }


    @Override
    public void onFailure(int code, String msg) {

    }

    @Override
    public void onWait() {

    }

    @Override
    public void onSuccess(Object data) {

    }
}
